package homework.day01;

import java.io.*;

/**
 * @Classname: HomeWork02
 * @Author: bromide
 * @CreateTime: 2022/3/28--19:47
 * @Version: V1.0
 * @Desciption: 将当前目录下的所有文件都复制一份，复制的文件命名为:原
 * 文件名_cp.后缀
 * 比如原文件为:test.dat
 * 复制后的文件为:test_cp.dat
 */
public class HomeWork02 {
    public static void main(String[] args) throws IOException {
        //创建需要操作的文件file
        File dir = new File("./src/homework/day02/Test01.java");
        if (dir.isDirectory()) {
            //筛选dir下层的文件（排除文件夹）
            File[] subs = dir.listFiles(file -> file.isFile());
            for (int i = 0; i < subs.length; i++) {
                String file = subs[i].getName();
                FileInputStream fis = new FileInputStream(file);
                String name1 = file.substring(0, file.lastIndexOf("."));
                String name2 = file.substring(file.lastIndexOf("."));
                String fileNew = name1 + "_cp." + name2;
                FileOutputStream fos = new FileOutputStream(fileNew);
                byte[] data = new byte[1024 * 10];
                int len;
                while ((len = fis.read(data)) != -1) {
                    fos.write(data, 0, len);
                }
                fis.close();
                fos.close();
            }
        }
        System.out.println("所有文件复制完成！");
    }
}
